package com.lylg.picture_share.controller;

import com.lylg.picture_share.entity.User;
import com.lylg.picture_share.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller

public class FrontUserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/front/login")
    public String doLogin(User user, HttpSession session) {

        User loginUser = userService.getUserByNameAndPwd(user);
        if (loginUser != null) {
            session.setAttribute("loginuser", loginUser);
            return "redirect:/front/index";
        } else {
            return "redirect:/front/index";

        }

    }

    @RequestMapping("/front/userCenterUI")
    public String goUserCenter() {


        return "/front/userCenter";

    }

    @RequestMapping("/front/getalluser")
    public String getAllUser(Model model,HttpSession session) {
        Integer userId=0;
        if(session.getAttribute("loginuser")!=null) {
            userId = ((User) session.getAttribute("loginuser")).getUser_id();
        }
        User users = userService.selectUserById(userId);
        model.addAttribute("userList", users);
        return "/front/user";
    }
}
